
<?php include_once(dirname(__FILE__) . '/../general/head.php'); ?>

<?php include_once(dirname(__FILE__) . '/../general/header.php'); ?>

<?php include_once('left.php'); ?>
<div id="page-wrapper">


    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Xác nhận đánh giá thái độ</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <!--        <div class="col-lg-12">
        
                    <div class="panel-group" id="accordion">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="panel-title">
                                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">Thống kê</a>
                                </h4>
                            </div>
                            <div id="collapseOne" class="panel-collapse collapse in">
                                <div class="panel-body">
                                    <table class="table table-bordered table-hover table-striped">
                                        <tr>
                                            <td>Need Improvement</td>
                                            <td><?php echo $ni . ' / ' . count($rows); ?></td>
                                        </tr>
                                        <tr>
                                            <td>Becoming Effective</td>
                                            <td><?php echo $be . ' / ' . count($rows); ?></td>
                                        </tr>
                                        <tr>
                                            <td>Effective</td>
                                            <td><?php echo $ef . ' / ' . count($rows); ?></td>
                                        </tr>
                                        <tr>
                                            <td>High Effective</td>
                                            <td><?php echo $he . ' / ' . count($rows); ?></td>
                                        </tr>
                                        <tr>
                                            <td>Exceptional</td>
                                            <td><?php echo $ex . ' / ' . count($rows); ?></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
        
                    </div>
        
                </div>-->

        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Danh sách
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="form-group">
                        <label>Đợt đánh giá</label>
                        <input type="text" name="name" class="form-control" style="width: 50%;" readonly="readonly" value="<?php echo $evaluationInfo['name']; ?>" />
                    </div>
                    
                    
                    <div class="col-lg-12">
                        Supervisor 
                        <select class="form-control display-inline" name="filter-ss" style="width: 200px;">
                            <option value="all">Tất cả</option>
                            <?php foreach ($supervisors as $eachSS) { ?>
                                <option value="<?php echo $eachSS['code']; ?>" <?php echo ($filterSS == $eachSS['code']) ? 'selected="selected"' : ''; ?>><?php echo $eachSS['name']; ?></option>
                            <?php } ?>
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Nhóm
                        <select class="form-control display-inline" name="filter-title" style="width: 100px;">
                            <option value="all">Tất cả</option>
                            <?php
                            foreach ($titles as $key => $value) {
                                ?>
                                <option value="<?php echo $value; ?>" <?php echo ($filterTitle == $value) ? 'selected="selected"' : ''; ?>><?php echo $key; ?></option>
                            <?php } ?>
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Thái độ
                        <select class="form-control display-inline" name="filter-att" style="width: 170px;">
                            <option value="all" <?php echo ($filterAtt == 'all') ? 'selected="selected"' : ''; ?>>Tất cả</option>
                            <option value="done" <?php echo ($filterAtt == 'done') ? 'selected="selected"' : ''; ?>>Đã đánh giá</option>
                            <option value="not" <?php echo ($filterAtt == 'not') ? 'selected="selected"' : ''; ?>>Chưa đánh giá</option>
                            <?php
                            foreach ($attRatings as $key => $value) {
                                ?>
                                <option value="<?php echo $value['id']; ?>" <?php echo ($filterAtt == $value['id']) ? 'selected="selected"' : ''; ?>><?php echo $value['name']; ?></option>
                            <?php } ?>
                        </select>
                        <br/><br/>
                        <input type="button" class="btn btn-default" value="Chọn tất cả" name="selectAll">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="button" class="btn btn-default" value="Bỏ chọn tất cả" name="selectNone">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="button" class="btn btn-primary" value="Confirm" name="multi-confirm">
                        <br><br>
                    </div>



                    <div class="col-lg-12">
                        <?php
                        if ($rows == false) {
                            ?>
                            Hiện tại không có kết quả nào!
                            <br>
                        <?php } else { ?>

                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="main-table">
                                    <thead>
                                        <tr>
                                            <th>Teamleader code</th>
                                            <th>Supervisor code</th>
                                            <th>RSM code</th>
                                            <!--<th width="40px">No</th>-->

                                            <th width="90px">Area</th>
                                            <th width="110px">Location</th>
                                            <th width="70px">Mã NV</th>
                                            <th width="50px">Title</th>
                                            <th>Họ và tên</th>

                                            <!--<th>Tên đợt</th>-->
                                            <th>Trạng thái</th>
                                            <th>Điều chỉnh</th>
                                            <th>Số điểm</th>
                                            <th>Xếp loại</th>
                                            <th>Ngày đánh giá</th>
                                            <th>Ngày xác nhận</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $i = 1;
                                        foreach ($rows as $row) {
                                            ?>
                                            <tr id="<?php echo $row['adr_id']; ?>">
                                                <td style="vertical-align: middle;"><?php echo $row['teamleader_code']; ?></td>
                                                <td style="vertical-align: middle;"><?php echo $row['supervisor_code']; ?></td>
                                                <td style="vertical-align: middle;"><?php echo $row['rsm_code']; ?></td>
                                                <!--<td><?php echo $i; ?></td>-->

                                                <td><?php echo $regionDic[$row['rsm_code']]; ?></td>
                                                <td><?php echo $row['location']; ?></td>
                                                <td><?php echo $row['code']; ?></td>
                                                <td><?php echo $row['title']; ?></td>
                                                <td><?php echo $row['name']; ?></td>

                                                <!--<td><?php echo $evaluationInfo['name']; ?></td>-->
                                                <td id="confirmStatus-<?php echo $row['adr_id']; ?>">
                                                    <?php if ($row['rated_day']) { ?>
                                                        <?php if ($row['confirmStatus'] == 'waiting' || $row['confirmStatus'] == 'review') { ?>
                                                                                                                                                                                    <!--<form method="post" action="<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_detail_results/confirm/' . $row['adr_id']); ?>">-->
                                                            <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_detail_results/do_confirm/' . $row['adr_id'] . '/confirm'); ?>" class="btn btn-primary btn-xs">Confirm</a>
                                                            <br>
                                                            <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_detail_results/do_confirm/' . $row['adr_id'] . '/reject'); ?>" class="btn btn-danger btn-xs">Reject</a>
                                                            <!--</form>-->
                                                            <?php
                                                        } else {
                                                            echo ucfirst($row['confirmStatus']);
                                                        }
                                                        ?>
                                                    <?php } ?>
                                                </td>
                                                <td id="edit-<?php echo $row['adr_id']; ?>">
                                                    <?php if (!$row['rated_day']) { ?>

                                                    <?php } else { ?>
                                                        <?php if ($row['confirmStatus'] == 'waiting' || $row['confirmStatus'] == 'review') { ?>
                                                            <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_user_details/edit2/' . $row['id']); ?>">Điều chỉnh</a>
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                </td>
                                                
                                                <td><?php echo $row['score']; ?></td>
                                                <td><?php echo color_attitude($row['score'], 200, 120, $row['rating']); ?></td>
                                                <td><?php echo $row['rated_day']; ?></td>
                                                
                                                <td id="confirmDay-<?php echo $row['adr_id']; ?>"><?php echo $row['confirm_day']; ?></td>


                                            </tr>
                                            <?php
                                            $i++;
                                        }
                                        ?>
                                    </tbody>
                                </table>

                            </div>
                        <?php } ?>
                    </div>
                    <br>
                    <div class="col-lg-12">
                        <input type="button" class="btn btn-default" value="Chọn tất cả" name="selectAll">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="button" class="btn btn-default" value="Bỏ chọn tất cả" name="selectNone">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="button" class="btn btn-primary" value="Confirm" name="multi-confirm">
                        <br><br>
                    </div>
<!--                    <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_detail_results/add'); ?>"> <img src="<?php echo base_url(); ?>images/plus.png" title="Thêm bảng đánh giá mới"> Thêm bảng đánh giá mới</a>
                    <br>-->
                </div>
                <!-- /.panel-body -->
            </div>
        </div>
    </div>


</div>
<script>
    function search()
    {
        var ss_code = "";
        var title = "";
        var att = "";

        $("select[name=filter-ss] option:selected").each(function() {
            ss_code += $(this).val();
        });
        $("select[name=filter-title] option:selected").each(function() {
            title = $(this).val();
        });
        $("select[name=filter-att] option:selected").each(function() {
            att = $(this).val();
        });
        window.location = '<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_detail_results/confirm') ?>' + '/' + ss_code + '/' + title + '/' + att;
    }

    $(document).ready(function() {
        var table = $('#main-table').dataTable({
            //"iDisplayLength": 50,
            "scrollX": true,
            "sScrollX": "120%",
            "order": [[12, "desc"]],
            "columnDefs": [
                {
                    "targets": [0],
                    "visible": false,
                    "searchable": false,
                    "orderData": [0, 1]
                },
                {
                    "targets": [1],
                    "visible": false,
                    "searchable": false,
                    "orderData": [0, 1]
                },
                {
                    "targets": [2],
                    "visible": false,
                    "searchable": false,
                    "orderData": [0, 1]
                },
            ]
        });

        var selected = [];
        $('#main-table tbody').on('click', 'tr', function() {
            var id = this.id;
            var index = $.inArray(id, selected);

            if (index === -1) { // chưa có trong selected
                selected.push(id);
            } else {
                selected.splice(index, 1);
            }

            $(this).toggleClass('success');
        });

        $("input[name=selectAll]").click(function() {
            var rows = $('#main-table tbody tr');
            selected = [];
            rows.each(function(index, item) {
                if ($(item).hasClass("success") == false)
                {
                    $(item).toggleClass('success');
                }
                if (item.id != '')
                {
                    selected.push(item.id);
                }
            });
        });
        
        $("input[name=selectNone]").click(function() {
            
            selected = [];
            var rows = $('#main-table tbody tr');
            rows.each(function(index, item) {
                if ($(item).hasClass("success") == true)
                {
                    $(item).toggleClass('success');
                }
            });
        });

        $("input[name=multi-confirm]").click(function() {
            if (selected.length > 0) {
                $.ajax({
                    url: '<?php echo site_url(get_folder($loggedUser['title']) . '/attitude_detail_results/do_multi_confirm/'); ?>',
                    type: 'post',
                    data: {'selecteds': selected},
                    success: function(data) {
                        if (data.success == "ok")
                        {
                            //var abc = JSON.parse(data);
                            for (i = 0; i < selected.length; i++) {
                                if($('#confirmStatus-' + selected[i]).text().indexOf('Rejected') < 0)
                                {
                                    $('#edit-' + selected[i]).html('');
                                    $('#confirmStatus-' + selected[i]).text('Confirmed');
                                    // không cần so sánh if gì hết, vì selected dc truyền về server theo thứ tự chọn, 
                                    // server cũng xử lý và trả về theo thứ tự chọn, nên for theo biến cục bộ selected và dùng index đó cho array data.results là ok
                                    $('#confirmDay-' + selected[i]).html(data.results[i].confirm_day);
                                }
                            }
                        }
                        else
                        {

                        }
                    },
                    error: function(xhr, status, strErr) {
                        alert(xhr + "\n" + strErr);
                    }
                });
            }
            else
            {
                alert('Bạn chưa chọn kết quả nào.');
            }
        });

        $('select[name=filter-ss]').change(function() {
            search();
        });

        $('select[name=filter-title]').change(function() {
            search();
        });
        
        $('select[name=filter-att]').change(function() {
            search();
        });
    });
</script>

<?php include_once(dirname(__FILE__) . '/../general/footer.php'); ?>
